package lx.utils;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

class DemoButton extends Button implements OnClickListener {

    private final static int MARGIN = 10;

    private final Context mContext;
    private final DemoButtonData mData;

    public DemoButton(Context context, DemoButtonData data) {
        super(context);
        this.mContext = context;
        this.mData = data;
        this.initView(context);
    }

    private void initView(Context context) {
        this.setTag(this.mData.mTitle); // so that we could find it later
        this.setText(this.mData.mTitle);
        this.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this.mContext, this.mData.mActivity);
        this.mContext.startActivity(intent);
    }

    public static void addToLinearLayout(LinearLayout parent, DemoButton button) {
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        llp.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
        parent.addView(button, llp);
    }
}

@SuppressWarnings("rawtypes")
class DemoButtonData {

    String mTitle;
    Class mActivity;

    DemoButtonData(String title, Class activity) {
        this.mTitle = title;
        this.mActivity = activity;
    }
}
